package com.yc.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.yc.springcloud.entity.Member;
import com.yc.springcloud.handler.CustomGlobalBlockHandler;
import com.yc.springcloud.service.MemberService;
import com.yc.springcloud.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.concurrent.TimeUnit;

/**
 * @Author DELL
 * @Date 2024/10/3 11:26
 * @Version 1.0
 */
@Slf4j
@Controller
public class MemberController {

	@Resource
	private MemberService memberService;
	//1. 我们的前端如果是以json格式来发送添加信息Member， 那么我们需要使用@RequestBody
	//   , 才能将数据封装到对应的bean, 同时保证http的请求头的 content-type是对应
	//2. 如果前端是以表单形式提交了，则不需要使用@RequestBody, 才会进行对象参数封装, 同时保证
	//   http的请求头的 content-type是对应

	// 增加新的流控规则
	@ResponseBody
	@GetMapping(value = "/t1")
    public Result t1(){
    	return Result.success("t1 执行");
    }

	@ResponseBody
	@GetMapping(value = "/t2")
	public Result t2(){
		return Result.success("t2 执行");
	}

	// 热点规则
	@ResponseBody
	@GetMapping(value = "/news")
	// blockHandler 是由sentinel控制 作用指定当接口被限流时调用的处理方发法
	@SentinelResource(value = "news",blockHandler = "newsBlockHandler")
	public Result quaryNews(@RequestParam(value = "id" ,required = false) String id,
	                        @RequestParam(value = "type",required = false) String type)
	{
		log.info("quaryNews");
		return Result.success("news");
	}

	//限流时的处理方法
	public Result newsBlockHandler(String id,
	                               String type,
	                               BlockException blockException){
		return Result.success("查询id=" + id + "新闻触发热点限流");
	}

	// 热点规则
	@ResponseBody
	@GetMapping(value = "/news1")
	// blockHandler 是由sentinel控制 作用指定当接口被限流时调用的处理方发法
	//使用全局处理
	@SentinelResource(value = "news1",
			blockHandlerClass = CustomGlobalBlockHandler.class,
			blockHandler = "handlerMethod")
	public Result quaryNews1(@RequestParam(value = "id" ,required = false) String id,
	                        @RequestParam(value = "type",required = false) String type)
	{

		log.info("quaryNews1");
		return Result.success("news1");

	}

	// 热点规则
	@ResponseBody
	@GetMapping(value = "/news2")
	// blockHandler 是由sentinel控制 作用指定当接口被限流时调用的处理方发法
	//使用全局处理
	@SentinelResource(value = "news2",
			fallbackClass = CustomGlobalBlockHandler.class,
			fallback = "fallbackHandlerMethod",
			blockHandlerClass = CustomGlobalBlockHandler.class,
			blockHandler = "handlerMethod")
	public Result quaryNews2(@RequestParam(value = "id" ,required = false) String id,
	                         @RequestParam(value = "type",required = false) String type)
	{

		log.info("quaryNews1");
		return Result.success("news1");

	}

	@ResponseBody
	@GetMapping(value = "/member/get/{id}")
	public Result queryMemberById(@PathVariable("id")  Long id){

		try {
			TimeUnit.MILLISECONDS.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("---休眠1s---");
		Member member = memberService.queryMemberById(id);
		if (member == null){
			return Result.error("402-10004",id + "不存在" );
		}else{
			return Result.success("查找成功-10004",member);
		}
	}

	@ResponseBody
	@PostMapping(value = "/member/saveMember")
	public Result save(@RequestBody Member member){
		int affected = memberService.saveMember(member);
		log.info("reset= " + affected);
		if (affected > 0) { //成功
			return Result.success("添加用户成功-10004", affected);
		} else {
			return Result.error("401-10004", "添加用户失败");
		}
	}
}
